package operator.function;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import util.RxJavaHelper;

class OnErrorResumeNextOperator {

    public static void main(String[] args) {
        onErrorResumeNext();
    }

    /**
     * 当发生错误时，onErrorResumeNext可以正常返回，但是支持自定义Observable
     */
    static void onErrorResumeNext(){
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(RxJavaHelper.getException());
            }
        })
                .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends Integer>>() {
                    @Override
                    public ObservableSource<? extends Integer> apply(Throwable throwable) throws Exception {
                        return Observable.just(3);
                    }
                })
                .subscribe(RxJavaHelper.getConsumer());
    }
}
